<?php

namespace App\Http\Validate\OpenShop\SiteLocation;

use App\Http\Validate\AbstractBaseValidate;
use App\Models\OpenShop\SiteLocation\IntendedSiteSelectionModel;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class IntendedSiteSelectionValidate extends AbstractBaseValidate
{
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [];
    }

    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [];
    }

    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }

    /**
     * 意向列表校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function listValidate(array $params)
    {
        return self::getInstance()->validate(
            ["pageSize", "pageNum"],
            $params,
            [
                "pageSize" => "required|int|min:1|max:100",
                "pageNum"  => "required|int|min:1"
            ]
        );
    }

    /**
     * 删除校验
     *
     * @param array $params
     * @param int $shopId
     * @param int $uid
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function delValidate(array $params, int $shopId, int $uid)
    {
        return self::getInstance()->validate(
            ["id"],
            $params,
            [
                "id" => [
                    "required",
                    "int",
                    Rule::exists((new IntendedSiteSelectionModel())->getTable(), "id")
                        ->where("isDeleted", 0)
                        ->where("createdByUid", $uid)
                        ->where("shopId", $shopId)
                ]
            ]
        );
    }

    /**
     * 意向址添加校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function insertValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                "poiName",
                "poiAddress",
                "provinceCode",
                "cityCode",
                "districtCode",
                "addressPrefix",
                "longitude",
                "latitude",
                "wgNumber",
                "type",
            ],
            $params,
            [
                "poiName"       => self::stringValidateBuild(false, 120, 1),
                "poiAddress"    => self::stringValidateBuild(false, 180, 1),
                "provinceCode"  => self::stringValidateBuild(false, 12, 1),
                "cityCode"      => self::stringValidateBuild(false, 12, 1),
                "districtCode"  => self::stringValidateBuild(false, 12, 1),
                "addressPrefix" => self::stringValidateBuild(false, 88, 1),
                "longitude"     => [
                    "string",
                    "regex:/^[\-\+]?(0?\d{1,2}(\.\d{1,6})*|1[0-7]?\d{1}(\.\d{1,6})*|180(\.0{1,6})*)$/"
                ],
                "latitude"      => [
                    "string",
                    "regex:/^[\-\+]?([0-8]?\d{1}(\.\d{1,6})*|90(\.0{1,6})*)$/"
                ],
                "wgNumber"      => [
                    "required",
                    "string"
                ],
                "type"      => [
                    "required",
                    "string"
                ]
            ]
        );
    }
}
